var NetTcp = (function () {
    function NetTcp() {

    }

    var __proto = NetTcp.prototype;

    __proto.loginGame = function () {
        console.log("登陆游戏")
        this.tcpLink = laya.tcpio.newConnect(
            appConfig.gameUrl,
            this.onTcpConnect.bind(this),
            this.onTcpDisconnect.bind(this),
            this.onTcpData.bind(this)
        );
    }
    __proto.onTcpDisconnect = function (sio) {
        if (this.finalEnd == 1) {
            console.log("牌局结束了不需要断线重连了");
            return;
        }

        if (this.kickFlag) {
            console.log("被踢下线不需要重连");
            return;
        }
        if (sio && sio.uuid != this.tcpLink.uuid) {
            console.log("uuid不相等")
            return;
        }
        if (this.tcpLink && this.tcpLink.sioConnected) {
            console.log("连接正常无需重新连接")
            return;
        }
        console.log("网络断开连接正在尝试重新连接");
        setTimeout(function () {
            this.loginGame();
        }.bind(this), 2000)
    }
    __proto.onTcpConnect = function () {
        var gameMsg = proto.lookup("pb.Msg");
        this.gameMsg = gameMsg;
        //this.tcpLink.updateDelay = this.refreshPingInfo.bind(this);

        var m1 = gameMsg.create({
            event: EventLogin,
            loginInfo: {
                id: userInfo.getToken()
            },
            enterRoom: {
                roomType: 1,
            }
        });
        this.send(m1);
    }
    __proto.onTcpData = function (data) {
        var event = data.event;
        console.log("event is ", event)
        console.log("data is ", data)

        if (data.event == EventError) {
            var info = data.error;
            if (info.err == "登录成功") {
                this.sendEnter();
                new UIPanel_Main()
            }
            if (info.err == "roomNil") {
                console.log("重新发送进房间指令");
                this.sendEnter();
            }
            console.log(info.err);
            return;
        }
        if (data.event == EventReEnter) {
            console.log("断线重连", data);

            console.log("进入房间", data.gameInfo);
            Observer.fire('initUserData', { msg: data.gameInfo });
            this.gameStatus(data.gameInfo.gameStatus);
            Observer.fire('updateData', { msg: data.gameInfo, self: data.gameInfo.self.gameStatus });
        }
        if (data.event == EventEnter) {

            console.log("进入房间", data.gameInfo);
            Observer.fire('initUserData', { msg: data.gameInfo });
            this.gameStatus(data.gameInfo.gameStatus);
            Observer.fire('updateData', { msg: data.gameInfo, self: data.gameInfo.self.gameStatus });
        }
        if (data.event == EventNotifyK3ARecord) {
            console.log("北京玩法");
            Observer.fire('updateGameType', { msg: "北京玩法" })
        }
        if (data.event == EventNotifyK3BRecord) {
            console.log("江苏玩法");
            Observer.fire('updateGameType', { msg: "江苏玩法" })
        }
        if (data.event == EventNotifyUserInfo) {
            console.log("玩家信息事件");
            this.gameStatus(data.gameInfo.gameStatus);
            Observer.fire('updateData', { msg: data.gameInfo, self: data.gameInfo.self.gameStatus });
        }
        if (data.event == EventNotifyUserBet) {
            console.log("通知玩家下注");
            Observer.fire('updateDataNoSelf', { msg: data.throwCoin, self: null });
        }
        if (data.event == EventNotifyGameResult) {
            console.log("游戏结果", data.gameInfo.winnerInfo);
            Observer.fire('updateGameWinner', { msg: data.gameInfo.winnerInfo.self.winScore })
            Observer.fire('updateKaijiang', { msg: data.gameInfo.res.cids })
            //Observer.fire('updateData', { msg: data.throwCoin, self: null });
        }

    }
    __proto.send = function (data) {
        try {
            if (this.tcpLink && this.tcpLink.sioConnected) {
                if (!this.gameMsg) {
                    this.gameMsg = proto.lookup("pb.Msg");
                }
                var buf = this.gameMsg.encode(data).finish();
                this.tcpLink.send(buf);
            }
        } catch (e) {
            console.log("websockt send err:", e);
        }
    }
    __proto.sendEnter = function () {
        // console.log("sendEnter")
        var m1 = this.gameMsg.create({
            event: EventEnter,
        })
        this.send(m1);
    }
    __proto.sendBet = function (data) {
        var action = {
            seatId: userInfo.getSeatId(),
            act: 0,
            bet: parseInt(data.bet),
            betType: data.betType,
            betChoose: data.betChoose,
        }
        var m1 = this.gameMsg.create({
            event: EventUserBet,
            action: action
        });
        console.log('下注', action);
        this.send(m1);
    }
    __proto.sendCancelBet = function (data) {
        var action = {
            seatId: userInfo.getSeatId(),
            act: 0,
            bet: parseInt(data.bet),
            betType: data.betType,
            betChoose: data.betChoose,
        }
        var m1 = this.gameMsg.create({
            event: EventCancelBet,
            action: action
        });
        console.log('取消下注', action);
        this.send(m1);
    }
    __proto.gameStatus = function (gameStatus) {
        switch (gameStatus) {
            case k3RoomStatusOnIdle:
                console.log("Idle");
                break;
            case k3RoomStatusWaitStart:
                console.log("判断游戏开始");
                Observer.fire('updateGameState', { msg: "开始游戏" })
                break;
            case k3RoomStatusBet:
                console.log("下注循环");
                Observer.fire('updateGameState', { msg: "下注中" })
                isBet = true;
                var self = this;
                Observer.regist('startBet', function (args) {
                    self.sendBet(args);
                })
                Observer.regist('cancelBet', function (args) {
                    self.sendCancelBet(args);
                })
                break;
            case k3RoomStatusEnd:
                isBet = false;
                console.log("开奖循环");
                Observer.fire('updateGameState', { msg: "开奖中" })
                Observer.remove('startBet');
                Observer.remove('cancelBet');
                break;
        }
    }









    NetTcp._instance = null;
    NetTcp.instance = function () {
        if (!NetTcp._instance) {
            NetTcp._instance = new NetTcp();
        }
        return NetTcp._instance;
    }
    return NetTcp;
})()
